#include <iostream>
#include "Timer.h"

namespace custom
{
    Timer::Timer() 
    {
#ifdef WIN32
        QueryPerformanceFrequency(&Hz);
        QueryPerformanceCounter(&start_time);        
#else// Linux
        gettimeofday(&start_time, nullptr);
#endif
    }

#ifdef WIN32
    LARGE_INTEGER Time::getStartTime() const
#else// Linux
    timeval Timer::getStartTime() const
#endif
    {
        return start_time;
    }

    Timer::~Timer()
    {
#ifdef WIN32
        LARGE_INTEGER end_time;
        QueryPerformanceCounter(&end_time);
        double time = (double)(end_time.QuadPart - start_time.QuadPart) / (double)Hz.QuadPart;
        std::cout << "It costs " << time * 1000 << " ms." << std::endl;
#else// Linux
        timeval end_time;
        gettimeofday(&end_time, nullptr);
        double time = (double)(end_time.tv_sec - start_time.tv_sec) + (double)(end_time.tv_usec - start_time.tv_usec) / 1000000;
        std::cout << "It costs " << time * 1000 << " ms." << std::endl;
#endif
    }
}

